
local name = "i26_laotie"
STRINGS.NAMES[string.upper(name)] = "老铁"
STRINGS.RECIPE_DESC[string.upper(name)]= "老铁"
STRINGS.CHARACTERS.GENERIC.DESCRIBE[string.upper(name)] = "这是老铁！"

local assets = {
    Asset("ANIM", "anim/i26_laotie.zip"),
    Asset("ANIM", "anim/swap_i26laotie.zip"),
    Asset("ANIM", "anim/i26_laotie_boomerang.zip"),
    
    Asset( "IMAGE", "images/iii/i26_laotie.tex" ),
    Asset( "ATLAS", "images/iii/i26_laotie.xml" ),
}



local function onequip(inst, owner)
    local lv = inst.components.i26wendu:GetLv()
    owner.AnimState:OverrideSymbol("swap_object", "swap_i26laotie", "swap_axe"..lv)
    
    --owner.AnimState:OverrideSymbol("swap_object", "a", "swap_axe")
    owner.AnimState:Show("ARM_carry")
    owner.AnimState:Hide("ARM_normal")

    if owner.components.planardefense == nil then
        owner:AddComponent("planardefense")
    end 
    -- if owner.components.i26skill ~= nil then
    --     owner.components.i26skill:OnActLaoTie()
    -- end
    inst.components.i26egotool:EqiuipFn(owner)

    inst.playerosalisfn = function()
        if owner then
            local lv = inst.components.i26wendu:GetLv()
            owner.AnimState:OverrideSymbol("swap_object", "swap_i26laotie", "swap_axe"..lv)
        end
    end
    inst:ListenForEvent("i26wenduchange", inst.playerosalisfn, owner)
end
local function onunequip(inst, owner)
    inst:RemoveEventCallback("i26wenduchange", inst.playerosalisfn, owner)
    owner.AnimState:Hide("ARM_carry")
    owner.AnimState:Show("ARM_normal")
    -- if owner.components.i26skill ~= nil then
    --     owner.components.i26skill:OnDeLaoTie(inst)
    -- end
    inst.components.i26egotool:UnEqiuipFn(owner)


    if inst.components.talker then
        --inst.components.talker:ShutUp()
        inst.components.talker:Say("")
    end
end

local function SayLv(inst)
    local talker = inst.components.talker
    local i26egotool = inst.components.i26egotool
    if talker == nil 
    or i26egotool == nil 
    then return end 

    local atk       = string.format("%d", i26egotool.damage)
    local planaratk = string.format("%d", i26egotool.planardamage)
    local def       = (i26egotool.def * 100) .. "%"
    local planardef = i26egotool.planardef
    local str = " 物理攻击: ".. atk .."\n 位面攻击: " .. planaratk .. "\n   物理防御: " .. def .. "\n位面防御: " .. planardef 
    if i26egotool.choplv then
        str = str .. "\n砍伐效率: " .. i26egotool.choplv*100 .. "%"
    end
    talker:Say(str)
end

-- aoetargeting
    local function ReticuleTargetFn()
        return Vector3(ThePlayer.entity:LocalToWorldSpace(11, 0, 0))
    end
    local function ReticuleMouseTargetFn(inst, mousepos)
        if mousepos ~= nil then
            local x, y, z = inst.Transform:GetWorldPosition()
            local dx = mousepos.x - x
            local dz = mousepos.z - z
            local l = dx * dx + dz * dz
            if l <= 0 then
                return inst.components.reticule.targetpos
            end
            l = 11 / math.sqrt(l)
            return Vector3(x + dx * l, 0, z + dz * l)
        end
    end
    local rSCALE = 1.5
    local function ReticuleUpdatePositionFn(inst, pos, reticule, ease, smoothing, dt)
        local x, y, z = inst.Transform:GetWorldPosition()
        reticule.Transform:SetPosition(x, 0, z)
        local rot = -math.atan2(pos.z - z, pos.x - x) / DEGREES
        if ease and dt ~= nil then
            local rot0 = reticule.Transform:GetRotation()
            local drot = rot - rot0
            rot = Lerp((drot > 180 and rot0 + 360) or (drot < -180 and rot0 - 360) or rot0, rot, dt * smoothing)
        end
        reticule.Transform:SetRotation(rot)
        
        if inst.i26_reticule == nil or not inst.i26_reticule:IsValid() then
            inst.i26_reticule = SpawnPrefab("reticuleaoe")
            inst.i26_reticule.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
        end

        local x2, y2, z2 = pos:Get()
        inst.i26_reticule.Transform:SetPosition(x2, 0, z2)

        local animmult = math.max(math.sqrt(distsq(x, z, x2, z2))/12, .1)
        reticule.AnimState:SetScale(rSCALE * animmult + .5, rSCALE)
    end
--
    local function SpellFn(inst, doer, pos)
        --doer.droplaotie = inst
        if not doer:HasTag("i26") then
            if doer.components.talker then
                doer.components.talker:Say("太重啦！")
            end
            return false
        end
        inst.components.i26egotool:Throw(doer, pos)
    end
--

local function onattack(inst, attacker, target)
    if not target:IsValid() then
        --target killed or removed in combat damage phase
        return
    end
    local fx = SpawnPrefab(math.random() < .5 and "shadowstrike_slash_fx" or "shadowstrike_slash2_fx")
    local x, y, z = target.Transform:GetWorldPosition()
    fx.Transform:SetPosition(x, y + 1.5, z)
    fx.Transform:SetRotation(inst.Transform:GetRotation())

    local lv = inst.components.i26wendu:GetLv()
    if lv == 4 and target.components.burnable ~= nil then
        target.components.burnable:Ignite(true, inst)
    elseif lv == 2 and target.components.freezable ~= nil then
        target.components.freezable:AddColdness(1)
        target.components.freezable:SpawnShatterFX()
        if target.components.freezable:IsFrozen() and target.components.combat then
            target.components.combat.externaldamagetakenmultipliers:SetModifier("i26laotieice", 2)
            if target.i26laotieystask ~= nil then
                target.i26laotieystask:Cancel()
                target.i26laotieystask = nil
            end
            target.i26laotieystask = target:DoTaskInTime(1, function()
                target.components.combat.externaldamagetakenmultipliers:RemoveModifier("i26laotieice")
            end)
        end
    end
end

local function sxdgfn(inst)
    inst.components.i26wendu:Clight()
end

local colourbiao = {
    zsq1 = { 1, .75, 0, 1 },
    zsq0 = { 60/255, 180/255, 110/255, 1 },
    zsq2 = { 1, .1, 0, 1 },
}

local function zsqysbd(inst)
    local type = inst.phlx:value()
    inst.components.aoetargeting.reticule.validcolour = colourbiao["zsq"..type] or colourbiao.zsq0
end

local function fn()
    local inst = CreateEntity()
    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()
    inst.entity:AddLight()

    MakeInventoryPhysics(inst, 300)

    inst:AddTag("weapon")
    inst:AddTag("meteor_protection")  
    inst:AddTag("nosteal")
    inst:AddTag("hide_percentage")
    inst:AddTag("i26_laotie")
    inst:AddTag("sharp") -- 声音
    inst:AddTag("shoreonsink")  --不掉深渊
    inst:AddTag("controlled_burner")  --可控点火者


    inst.Light:SetRadius(1.2)
    inst.Light:SetFalloff(.5)
    inst.Light:SetIntensity(.5)
    inst.Light:SetColour(235 / 255, 165 / 255, 12 / 255)
    inst.Light:Enable(false)
    inst.Light:EnableClientModulation(true)

    inst.AnimState:SetBank("i26_laotie")
    inst.AnimState:SetBuild("i26_laotie")
    inst.AnimState:PlayAnimation("ground_0")

    inst.AnimState:AddOverrideBuild("boomerang01")
    inst.AnimState:SetLightOverride(.2)

    inst:AddComponent("talker")
    inst.components.talker.fontsize = 20
    inst.components.talker.font = TALKINGFONT
    inst.components.talker.colour = Vector3(0.7, 0.85, 1, 1)
    inst.components.talker.offset = Vector3(240, -120, 0)

    inst:AddComponent("aoetargeting")
    inst.components.aoetargeting:SetAlwaysValid(true)
    inst.components.aoetargeting.reticule.reticuleprefab = "reticuleline" --"reticulelongmulti" --"reticulelong"
    inst.components.aoetargeting.reticule.pingprefab = "reticulelineping"
    -- inst.components.aoetargeting.reticule.reticuleprefab = "reticuleaoe" --"reticuleaoe_1_6"
	-- inst.components.aoetargeting.reticule.pingprefab = "reticuleaoeping" --"reticuleaoeping_1_6"
    inst.components.aoetargeting.reticule.targetfn = ReticuleTargetFn
    -- inst.components.aoetargeting.reticule.mousetargetfn = ReticuleMouseTargetFn
    inst.components.aoetargeting.reticule.updatepositionfn = ReticuleUpdatePositionFn
    inst.components.aoetargeting.reticule.validcolour = { 60/255, 180/255, 110/255, 1 }
    inst.components.aoetargeting.reticule.invalidcolour = { .5, 0, 0, 1 }
    inst.components.aoetargeting.reticule.ease = true
    inst.components.aoetargeting.reticule.mouseenabled = true
    inst.components.aoetargeting:SetRange(32) --8格

    --inst.components.aoetargeting:SetDeployRadius(1)

    inst.phlx = net_tinybyte(inst.GUID, "i26laotie.phlx", "phlxdirty")

    inst:ListenForEvent("phlxdirty", zsqysbd)

    -- inst:AddComponent("bloomer")
    -- inst.components.bloomer:PushBloom("fx", "shaders/anim.ksh", 1)

    inst.entity:SetPristine()
    if not TheWorld.ismastersim then
        return inst
    end
    --inst.components.aoetargeting:SetTargetFX("reticuleaoesummontarget_1")

    inst:AddComponent("inspectable")
    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.imagename = "i26_laotie_0"
    inst.components.inventoryitem.atlasname = "images/iii/i26_laotie.xml"
    inst.components.inventoryitem:SetOnPutInInventoryFn(sxdgfn)

    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip(onequip)
    inst.components.equippable:SetOnUnequip(onunequip)
    inst.components.equippable.walkspeedmult = 0.8
    inst:AddComponent("weapon")
    inst.components.weapon:SetRange(2, 3)
    inst.components.weapon:SetOnAttack(onattack)

    inst:AddComponent("aoespell")
    inst.components.aoespell:SetSpellFn(SpellFn)

    inst:AddComponent("tool")
    inst.components.tool:SetAction(ACTIONS.CHOP, 1)

    inst:AddComponent("heater")
    inst.components.heater:SetThermics(false, false)
    inst:AddComponent("i26egotool")
    inst:AddComponent("i26wendu")
    

    inst:ListenForEvent("ondropped", sxdgfn)

    inst.SayLv = SayLv

    MakeHauntableLaunch(inst)

    return inst
end



local ret = {
    Prefab(name, fn, assets),
}


return unpack(ret)